import { Engine } from "../engine";  
import { Script,ScriptActionType } from "./script-interface";  
import { ScriptPlayer } from "./script-player";  
  
export class ScriptManager {  
  private engine: Engine;  
  private scripts: Map<string, Script> = new Map();  
  private scriptPlayer: ScriptPlayer;  
  
  constructor(engine: Engine) {  
    this.engine = engine;  
    this.scriptPlayer = new ScriptPlayer(engine);  
    this.loadDefaultScripts();  
  }  
  
  private loadDefaultScripts(): void {  
    // 加载默认剧本  
    const introScript: Script = {  
      id: "intro",  
      name: "开场剧本",  
      duration: 10000,  
      actions: [  
        {  
          type: ScriptActionType.DIALOGUE,  
          timestamp: 0,  
          data: {  
            text: "欢迎来到游戏世界！",  
            duration: 2000  
          }  
        },  
        {  
          type: ScriptActionType.CAMERA_ZOOM,  
          timestamp: 2500,  
          data: {  
            targetZoom: 1.5,  
            duration: 2000,  
            focusOnPlayer: true  
          }  
        },  
        {  
          type: ScriptActionType.DIALOGUE,  
          timestamp: 5000,  
          data: {  
            text: "让我们开始冒险吧！",  
            duration: 2000  
          }  
        },  
        {  
          type: ScriptActionType.CAMERA_ZOOM,  
          timestamp: 7500,  
          data: {  
            targetZoom: 1.0,  
            duration: 1500,  
            focusOnPlayer: true  
          }  
        }  
      ]  
    };  
  
    this.addScript(introScript);  
  }  
  
  addScript(script: Script): void {  
    this.scripts.set(script.id, script);  
  }  
  
  playScript(scriptId: string): boolean {  
    const script = this.scripts.get(scriptId);  
    if (script) {  
      this.scriptPlayer.playScript(script);  
      return true;  
    }  
    console.warn(`剧本未找到: ${scriptId}`);  
    return false;  
  }  
  
  stopCurrentScript(): void {  
    this.scriptPlayer.stopScript();  
  }  
  
  update(): void {  
    this.scriptPlayer.update();  
  }  
  
  get isPlaying(): boolean {  
    return this.scriptPlayer.isScriptPlaying;  
  }  
  
  get currentScript(): string | null {  
    return this.scriptPlayer.currentScriptName;  
  }  
  
  listScripts(): string[] {  
    return Array.from(this.scripts.keys());  
  }  
}